package com.recruit.server.service.impl;

import com.alibaba.fastjson.JSON;
import com.recruit.server.common.Constant;
import com.recruit.server.common.HeaderInfo;
import com.recruit.server.common.utils.CookieUtil;
import com.recruit.server.common.utils.MD5Util;
import com.recruit.server.common.utils.ToolUtil;
import com.recruit.server.dao.UserMapper;
import com.recruit.server.entity.pojo.User;
import com.recruit.server.request.user.LogOutRequest;
import com.recruit.server.request.user.LoginRequest;
import com.recruit.server.request.user.RegisterRequest;
import com.recruit.server.response.product.ProductDetailResponse;
import com.recruit.server.response.user.LogOutResponse;
import com.recruit.server.response.user.LoginResponse;
import com.recruit.server.response.user.RegisterResponse;
import com.recruit.server.service.IUserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.Date;

@Slf4j
@Service
public class UserServiceImpl implements IUserService {

    @Autowired
    private UserMapper userMapper;

    @Override
    public LoginResponse login(LoginRequest request, HttpSession httpSession) {
        LoginResponse response = new LoginResponse();
        try {
            //check if exist username
            int resultCount = userMapper.checkUsername(request.getUsername());
            if (resultCount == 0) {
                response.setCodeAndMessage(-1, "account is not exist");
                return response;
            }
            //md5加密
            String md5Password = MD5Util.MD5EncodeUtf8(request.getPassword());
            //check md5Password
            User user = userMapper.selectLogin(request.getUsername(), md5Password);
            if (user == null) {
                response.setCodeAndMessage(-1, "password is error");
                return response;
            }
            //login success and write user info to session
            HeaderInfo headerInfo = new HeaderInfo();
            headerInfo.setUserId(user.getId());
            headerInfo.setTimestamp(System.currentTimeMillis() + Constant.EXPIRE_TIME);
            httpSession.setAttribute(Constant.CURRENT_USER, headerInfo);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            response.setCodeAndMessage(-1, e.getMessage());
        }
        return response;
    }

    @Override
    public RegisterResponse register(RegisterRequest request, HeaderInfo headerInfo) {
        RegisterResponse response = new RegisterResponse();
        try {
            Date now = new Date();
            //check if exist username, if exist not need to register
            int resultCount = userMapper.checkUsername(request.getUserName());
            if (resultCount > 0) {
                response.setCodeAndMessage(-1, "user is exist");
                return response;
            }
            //add user info to mysql
            User user = new User();
            user.setUsername(request.getUserName());
            String md5Password = MD5Util.MD5EncodeUtf8(request.getPassword());
            user.setPassword(md5Password);
            user.setEmail(request.getEmail());
            user.setRole(1);
            user.setCreateTime(now);
            user.setUpdateTime(now);
            //插入sql语句，把上面的user对象里的字段值插入mysql
            userMapper.insert(user);
            response.setMsg("register success");
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            response.setCodeAndMessage(-1, e.getMessage());
        }
        return response;
    }

    @Override
    public LogOutResponse logOut(HttpServletRequest servletRequest, HttpSession session) {
        LogOutResponse response = new LogOutResponse();
        try {
            // delete user info from httpSession
            session.removeAttribute(Constant.CURRENT_USER);
        } catch (Exception e) {
            log.error(e.getMessage(), e);
            response.setCodeAndMessage(-1, e.getMessage());
        }
        return response;
    }
}
